import { InMemoryCache } from "../utils/cach";

const cache = new InMemoryCache<number|null>(24 * 60 * 60 * 1000);

export async function fetchPrice(symbol: string): Promise<number | null> {

    return await cache.get(symbol, async () => {
      try {
        const response = await fetch(`https://iss.moex.com/iss/engines/stock/markets/shares/securities/${symbol}.json`);
        const data = await response.json();
        const lastRow = data.securities.data.at(-1)

        return lastRow[22];
      } catch (error) {
      console.error('Error fetching data:', error);

      return null;
    }
  });
}